package com.zshan.clinic.web.controller.stock;


import com.zshan.clinic.admin.entity.dto.StockOrderPurchaseDTO;
import com.zshan.clinic.admin.entity.dto.StockOrderReqDTO;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.service.stock.StockOrderService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.StockOrderPurchase;
import com.zshan.clinic.database.model.StockOrderReq;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 药品档案
 */
@RestController
@RequestMapping("/stock/order")
public class StockOrderController extends AbstractController {


    @Autowired
    private StockOrderService stockOrderService;


    /**
     * 查询领用单列表
     * @param orderNo 订单编号
     * @param orderStatus 订单状态 ReqOrderStatusEnum
     * @param deptId 领用部门
     * @param storageId 药库ID
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getOrderReqList")
    public RestResp<PageInfo<StockOrderReqPageVo>> getOrderReqList(@RequestParam(name = "orderNo", required = false) String orderNo,
                                                                   @RequestParam(name = "orderStatus", required = false) String orderStatus,
                                                                   @RequestParam(name = "deptId", required = false) Long deptId,
                                                                   @RequestParam(name = "storageId", required = false) Long storageId,
                                                                   @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                   @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<StockOrderReqPageVo> reqList = stockOrderService.getOrderReqList(userInfo.getClinicId(), orderNo,orderStatus,deptId,storageId, pageNo, pageSize);
        return RestResp.SUCCESS(reqList);
    }

    /**
     * 添加领用单
     * @return
     */
    @PostMapping("/addReq")
    public RestResp<Void> addReq(@RequestBody StockOrderReqDTO stockOrderReqDTO) {
        UserInfo userInfo = getUserInfo();
        stockOrderReqDTO.setCreateUserId(userInfo.getEmpId());
        stockOrderReqDTO.setCreateBy(userInfo.getEmpName());
        stockOrderReqDTO.setClinicId(userInfo.getClinicId());
        stockOrderReqDTO.setInUserId(userInfo.getEmpId());
        stockOrderReqDTO.setInUserName(userInfo.getEmpName());
        stockOrderService.addReqDTO(stockOrderReqDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 查询领用单详情
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderReqDetail")
    public RestResp<StockOrderReqVo> getOrderReqDetail(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        StockOrderReqVo orderReqDetail = stockOrderService.getOrderReqDetail(userInfo.getClinicId(), orderId);
        return RestResp.SUCCESS(orderReqDetail);
    }


    /**
     * 审核领用单
     * @param orderReq
     * @return
     */
    @PostMapping("/reviewReq")
    public RestResp<Void> reviewReq(@RequestBody StockOrderReq orderReq) {
        UserInfo userInfo = getUserInfo();
        orderReq.setAuditUserId(userInfo.getEmpId());
        orderReq.setAuditUserName(userInfo.getEmpName());
        orderReq.setClinicId(userInfo.getClinicId());
        stockOrderService.reviewReq(orderReq);
        return RestResp.SUCCESS();
    }

    /**
     * 查询采购单列表
     * @param orderNo 订单号
     * @param orderStatus 订单状态  PurchaseOrderStatusEnum
     * @param settleStatus 结算状态 SettleStatusEnum
     * @param storageId 药库
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getOrderPurchaseList")
    public RestResp<PageInfo<StockOrderPurchasePageVo>> getOrderPurchaseList(@RequestParam(name = "orderNo", required = false) String orderNo,
                                                                             @RequestParam(name = "orderStatus", required = false) String orderStatus,
                                                                             @RequestParam(name = "settleStatus", required = false) String settleStatus,
                                                                             @RequestParam(name = "storageId", required = false) Long storageId,
                                                                             @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                             @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<StockOrderPurchasePageVo> purchaseList = stockOrderService.getOrderPurchaseList(userInfo.getClinicId(), orderNo,orderStatus,settleStatus,storageId, pageNo, pageSize);
        return RestResp.SUCCESS(purchaseList);
    }

    /**
     * 添加采购单
     * @return
     */
    @PostMapping("/addPurchase")
    public RestResp<Void> addPurchase(@RequestBody StockOrderPurchaseDTO purchase) {
        UserInfo userInfo = getUserInfo();
        purchase.setCreateUserId(userInfo.getEmpId());
        purchase.setCreateBy(userInfo.getEmpName());
        purchase.setClinicId(userInfo.getClinicId());
        purchase.setInUserId(userInfo.getEmpId());
        stockOrderService.addPurchaseDTO(purchase);
        return RestResp.SUCCESS();
    }

    /**
     * 查询采购单详情
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderPurchaseDetail")
    public RestResp<StockOrderPurchaseVo> getOrderPurchaseDetail(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        StockOrderPurchaseVo purchaseDetail = stockOrderService.getOrderPurchaseDetail(userInfo.getClinicId(), orderId);
        return RestResp.SUCCESS(purchaseDetail);
    }


    /**
     * 审核采购单
     * @param purchase
     * @return
     */
    @PostMapping("/reviewPurchase")
    public RestResp<Void> reviewPurchase(@RequestBody StockOrderPurchase purchase) {
        UserInfo userInfo = getUserInfo();
        purchase.setAuditUserId(userInfo.getEmpId());
        purchase.setAuditUserName(userInfo.getEmpName());
        purchase.setClinicId(userInfo.getClinicId());
        stockOrderService.reviewPurchase(purchase);
        return RestResp.SUCCESS();
    }

    /**
     * 结算采购单
     * @param purchase
     * @return
     */
    @PostMapping("/settlePurchase")
    public RestResp<Void> settlePurchase(@RequestBody StockOrderPurchase purchase) {
        UserInfo userInfo = getUserInfo();
        purchase.setSettleUserId(userInfo.getEmpId());
        purchase.setSettleUserName(userInfo.getEmpName());
        purchase.setClinicId(userInfo.getClinicId());
        stockOrderService.settlePurchase(purchase);
        return RestResp.SUCCESS();
    }

    /**
     * 退款采购单
     * @param purchase
     * @return
     */
    @PostMapping("/refusePurchase")
    public RestResp<Void> refusePurchase(@RequestBody StockOrderPurchase purchase) {
        UserInfo userInfo = getUserInfo();
        purchase.setSettleUserId(userInfo.getEmpId());
        purchase.setSettleUserName(userInfo.getEmpName());
        purchase.setClinicId(userInfo.getClinicId());
        stockOrderService.refusePurchase(purchase);
        return RestResp.SUCCESS();
    }

    /**
     * 查询仓库订单变动流水列表
     * @param orderId 订单ID
     * @param orderType 订单类型 stockOrderTypeEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getOrderLogList")
    public RestResp<PageInfo<StockOrderLogVo>> getOrderLogList(@RequestParam(name = "orderId") Long orderId,
                                                               @RequestParam(name = "orderType") String orderType,
                                                               @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<StockOrderLogVo> pageInfo = stockOrderService.getOrderLogList(userInfo.getClinicId(), orderId,orderType, pageNo, pageSize);
        return RestResp.SUCCESS(pageInfo);
    }

}
